package pe.gob.sedapal.scmultimedia.rcp.view.servicios;


import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.wb.swt.ResourceManager;

import java.awt.Color;
import java.awt.ScrollPane;
import javax.swing.BorderFactory;

import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.SWT;
import org.jpedal.PdfDecoder;
/*import org.jpedal.pdf.plugins.eclipse.editors.BookmarkPopup;
import org.jpedal.pdf.plugins.eclipse.editors.InfoPopup;
import org.jpedal.pdf.plugins.eclipse.editors.PDFEditor;*/
import org.jpedal.utils.LogWriter;

import pe.gob.sedapal.scmultimedia.rcp.action.EdgePageAction;
import pe.gob.sedapal.scmultimedia.rcp.action.MovePageAction;
import pe.gob.sedapal.scmultimedia.rcp.action.ScaleInAction;
import pe.gob.sedapal.scmultimedia.rcp.action.ScaleOutAction;
import pe.gob.sedapal.scmultimedia.rcp.Activator;
import pe.gob.sedapal.scmultimedia.util.Messages;

import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.wb.swt.SWTResourceManager;


public class PdfView extends ViewPart {

	
	public static final String ID = "scmultimedia.pdfView";

	private float scaling = .5f;
	private Composite viewer;
	private PdfDecoder decodePDF=new PdfDecoder();
	private int currentPage = 1;	
	
	MovePageAction forwardAction;
	MovePageAction nroPagina;
	
	private java.awt.Frame frame ;	
	private int pagActual = 0;
	private Label lblTituloRequisitos;
	
	public PdfView() {
	}

	@Override
	public void createPartControl(Composite parent) {
		// TODO Auto-generated method stub
	try{
			parent.setLayout(new FormLayout());
			
			viewer = new Composite(parent, SWT.EMBEDDED | SWT.NO_BACKGROUND);
			FormData fd_viewer = new FormData();
			fd_viewer.bottom = new FormAttachment(100, -6);
			fd_viewer.right = new FormAttachment(0, 772);
			fd_viewer.left = new FormAttachment(0);
			viewer.setLayoutData(fd_viewer);
			viewer.setBounds(10, 10, 228, 360);
			createToolbar(parent);
			decodePDF.setPDFBorder(BorderFactory.createLineBorder(Color.black, 1));
			decodePDF.setInset(5,5); 

			/** setup GUI and wrapper around SWING component - 
			 * does not work on MAC until AWT bridge bug fixed */

			frame = SWT_AWT.new_Frame(viewer);
			ScrollPane scrollPane = new ScrollPane();
			scrollPane.add(decodePDF);
			frame.add(scrollPane);
			frame.pack();
			
			Label imgLogoSedapal = new Label(parent, SWT.NONE);
			fd_viewer.top = new FormAttachment(imgLogoSedapal, 22);
			FormData fd_imgLogoSedapal = new FormData();
			fd_imgLogoSedapal.bottom = new FormAttachment(100, -460);
			fd_imgLogoSedapal.right = new FormAttachment(100, -21);
			imgLogoSedapal.setLayoutData(fd_imgLogoSedapal);
		
			imgLogoSedapal.setImage(ResourceManager.getPluginImage("scmultimedia", "icons/logoSedapalInterno.png"));
			imgLogoSedapal.setBounds(635, 10, 192, 84);
			
			lblTituloRequisitos = new Label(parent, SWT.NONE);
			lblTituloRequisitos.setForeground(SWTResourceManager.getColor(51, 102, 153));
			lblTituloRequisitos.setFont(SWTResourceManager.getFont("Ubuntu", 13, SWT.BOLD | SWT.ITALIC));
			FormData fd_lblTituloRequisitos = new FormData();
			fd_lblTituloRequisitos.bottom = new FormAttachment(viewer, -24);
			fd_lblTituloRequisitos.right = new FormAttachment(0, 286);
			fd_lblTituloRequisitos.left = new FormAttachment(0, 10);
			lblTituloRequisitos.setLayoutData(fd_lblTituloRequisitos);
			lblTituloRequisitos.setText("REQUISITOS PARA EL SERVICIO:");
		
			
		}catch(Exception e){
			LogWriter.writeLog("Exception "+e+" in createPartControl >> PDFViewer");
			e.printStackTrace();
		}catch(Error e){
			LogWriter.writeLog("Error "+e+" in createPartControl >> PDFViewer");
			e.printStackTrace();
		}
	 
	}

	public void createToolbar(Composite viewer){
		
		//AGREGAR ESPACIO
		MovePageAction espacio = new MovePageAction();
		espacio.setText("                                                                      ");
		getViewSite().getActionBars().getToolBarManager().add(espacio);		
		
		ScaleOutAction lCustomAction = new ScaleOutAction();
		lCustomAction.setImageDescriptor(Activator.getImageDescriptor("/icons/smminus.gif"));  
		getViewSite().getActionBars().getToolBarManager().add(lCustomAction);
		
		//AGREGAR ESPACIO
		MovePageAction espacio1 = new MovePageAction();
		espacio1.setText("   ");
		getViewSite().getActionBars().getToolBarManager().add(espacio1);
				
		ScaleInAction scaleInAction = new ScaleInAction();
		scaleInAction.setImageDescriptor(Activator.getImageDescriptor("/icons/smplus.gif"));  
		getViewSite().getActionBars().getToolBarManager().add(scaleInAction);
		
		//AGREGAR ESPACIO
		MovePageAction espacio2 = new MovePageAction();
		espacio2.setText("   ");
		getViewSite().getActionBars().getToolBarManager().add(espacio2);
		
		EdgePageAction firstPageAction = new EdgePageAction(1);
		firstPageAction.setImageDescriptor(Activator.getImageDescriptor("/icons/smstart.gif"));  
		getViewSite().getActionBars().getToolBarManager().add(firstPageAction);
		
		//AGREGAR ESPACIO
		MovePageAction espacio3 = new MovePageAction();
		espacio3.setText("   ");
		getViewSite().getActionBars().getToolBarManager().add(espacio3);
				
		MovePageAction backAction = new MovePageAction(-1,this);
		backAction.setImageDescriptor(Activator.getImageDescriptor("/icons/flecha1.jpg"));  
		getViewSite().getActionBars().getToolBarManager().add(backAction);
		
		//AGREGAR ESPACIO
		MovePageAction espacio4 = new MovePageAction();
		espacio4.setText("   ");
		getViewSite().getActionBars().getToolBarManager().add(espacio4);
		
		forwardAction = new MovePageAction(1,this);
		forwardAction.setImageDescriptor(Activator.getImageDescriptor("/icons/flecha2.jpg"));
		getViewSite().getActionBars().getToolBarManager().add(forwardAction);
		
		//AGREGAR ESPACIO
		MovePageAction espacio5 = new MovePageAction();
		espacio5.setText("   ");
		getViewSite().getActionBars().getToolBarManager().add(espacio5);
		
		EdgePageAction lastPageAction = new EdgePageAction(9);
		lastPageAction.setImageDescriptor(Activator.getImageDescriptor("/icons/smend.gif"));  
		getViewSite().getActionBars().getToolBarManager().add(lastPageAction);
		
		//AGREGAR ESPACIO
		MovePageAction espacio6 = new MovePageAction();
		espacio6.setText("   ");
		getViewSite().getActionBars().getToolBarManager().add(espacio6);
				
		//NUMERO DE PÁGINA
		nroPagina = new MovePageAction();
		nroPagina.setText(Messages.pdf_pagina+"1");
		getViewSite().getActionBars().getToolBarManager().add(nroPagina);
		//getViewSite().getActionBars().getToolBarManager().add(nroPagina);
	
 }

	public PdfView getPdfView(){
		return this;
	}
	
	public void setFocus() {
		viewer.setFocus();
		decodePDF.repaint();
	}

	public void dispose() {
		viewer.dispose();
		super.dispose();
	}

	public Composite getViewer() {
		return viewer;
	}


	public PdfDecoder getPDF() {
		return decodePDF;
	}


	public void repaint() {
		decodePDF.invalidate();
		frame.repaint();
	}


	public void openPDF(String fileName) {

		currentPage=1;
		scaling=.5f;
		try {
			decodePDF.openPdfFile(fileName);
			decodePDF.setPageParameters(scaling, currentPage); 
			decodePDF.decodePage(currentPage);
			repaint();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	public void zoom(float scale) {

		scaling=scaling*scale;
		decodePDF.setPageParameters(scaling, currentPage); 
		decodePDF.updateUI();
		repaint();

	}

	public void changePage(int pageChange) {
		
		int newPage=currentPage+pageChange;
		
		/**check page in range*/
		if((pageChange==-1 && currentPage>1) || (pageChange==1 && currentPage < decodePDF.getPageCount()-1)||
				(pageChange==-10 && currentPage>10) || (pageChange==10 && currentPage < decodePDF.getPageCount()-10)){

			currentPage=newPage;
			try {
				decodePDF.getTextLines().clearHighlights();
				//decodePDF.addHighlights(null,false);
				decodePDF.setPageParameters(scaling, currentPage); 
				decodePDF.decodePage(currentPage);
				decodePDF.updateUI();
				repaint();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}


	public float getScaling() {
		return scaling;
	}


	public void setScaling(float scaling) {
		this.scaling = scaling;
	}


	public PdfDecoder getDecodePDF() {
		return decodePDF;
	}


	public void setDecodePDF(PdfDecoder decodePDF) {
		this.decodePDF = decodePDF;
	}


	public int getCurrentPage() {
		return currentPage;
	}


	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getPagActual() {
		return pagActual;
	}

	public void setPagActual(int pagActual) {
		this.pagActual = pagActual;
	}
	
	public void actualizarPagina(int pag){		
		
		nroPagina.setText(Messages.pdf_pagina+pag);	
		getViewSite().getActionBars().getToolBarManager().update(true);
	
	}
}
